package com.test.demo.springbootdemo.service;

import com.test.demo.springbootdemo.entity.User;
import com.test.demo.springbootdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserServiceOther userServiceOther;

    public List<User> userList() {
        return userMapper.selectList(null);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public User updateById(Long id, Integer age) throws Exception {
        User user = userMapper.selectById(id);
        user.setAge(age);
        userMapper.updateById(user);
        userServiceOther.updateById2(id,age);
        if (id!=null) {
            throw new Exception("故意的");
        }
        return userMapper.selectById(id);
    }
}
